<?php

class Core_Form_Admin_Settings_Abstract extends Nuo_Form
{

	public function commitSetting($site_id)
	{
		if ($site_id == NULL)
		{
			$site_id = Nuo::getSiteId();
		}
		
		$table = new Core_Model_DbTable_Settings;

		$eles = $this -> getElements();
		
		foreach ($eles as $ele)
		{
			$name = $ele -> getName();
			if (substr($name, 0, 1) == '_')
			{
				continue;
			}
			$key = str_replace('_', '.', $name);
			$value = $ele->getValue();
			$table->setSetting($key, $value, $site_id);
		}
		$this->addMessage('saved change!');
	}

	public function populateSetting($site_id = null)
	{
		if ($site_id == NULL)
		{
			$site_id = Nuo::getSiteId();
		}
		
		$table = new Core_Model_DbTable_Settings;

		$eles = $this -> getElements();
		
		foreach ($eles as $ele)
		{
			$name = $ele -> getName();
			if (substr($name, 0, 1) == '_')
			{
				continue;
			}
			$key = str_replace('_', '.', $name);
			
			$value = $table->getSetting($key, null, $site_id);
			if ($value !== null)
			{
				$ele -> setValue($value);
			}
		}
	}

}
